<?php
	//这是中文
	header ('Access-Control-Allow-Origin: *');
	header ('Access-Control-Allow-Methods: GET, POST');

	include_once('config.php');
	include_once('saetv2.ex.class.php');

	$oauth = new SaeTOAuthV2(WB_AKEY, WB_SKEY);

	if (isset($_REQUEST['code'])) {
		$keys = array();
		$keys['code'] = $_REQUEST['code'];
		$keys['redirect_uri'] = WB_CALLBACK_URL;

		try {
			$token = $oauth->getAccessToken('code', $keys);
		} catch (OAuthException $e) {print_r($e);}
	}

	if ($token) {
		$access_token = $token["access_token"];
		$client = new SaeTClientV2(WB_AKEY, WB_SKEY, $access_token);
		$uid_get = $client->get_uid();
		$uid = $uid_get['uid'];

		$result = array (
							"result" => "success",
							"access_token" => $access_token,
							"uid" => $uid
		);

		echo "<script>window.parent.postMessage('" . json_encode($result) . "', '*');</script>";
	} else {
		$result = array (
							"result" => "failed"
		);

		echo "<script>window.parent.postMessage('" . json_encode($result) . "', '*');</script>";
	}
?>